package com.project.thisDatabase.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@ConditionalOnProperty(name = "spring.data.redis.enable", havingValue = "true")
@Configuration
@Component
public class RedisConfig {

    @Value(value = "${spring.data.redis.database:}")
    private String dbIndex;

    @Value(value = "${spring.data.redis.host:}")
    private String hostName;

    @Value(value = "${spring.data.redis.port:}")
    private String port;

    @Value(value = "${spring.data.redis.password:}")
    private String password;

    @Value(value = "${spring.data.redis.lettuce.pool.max-idle:}")
    private String maxIdle;

    @Value(value = "${spring.data.redis.lettuce.pool.min-idle:}")
    private String minIdle;

    @Value(value = "${spring.data.redis.lettuce.pool.max-active:}")
    private String maxActive;

    @Value(value = "${spring.data.redis.lettuce.pool.max-wait:}")
    private String maxWait;

    @Value(value = "${spring.data.redis.timeout:}")
    private String timeOut;

    @Value(value = "${spring.data.redis.timeout:}")
    private String shutdownTimeOut;

    @Bean
    public RedisTemplate<Object, Object> redisTemplate() {
//        RedisTemplate redisTemplate = new StringRedisTemplate(redisConnectionFactory);
//        StringRedisSerializer stringRedisSerializer =new StringRedisSerializer();
//        redisTemplate.setValueSerializer(stringRedisSerializer);
//        redisTemplate.setKeySerializer(stringRedisSerializer);
//        redisTemplate.setHashKeySerializer(stringRedisSerializer);
//        redisTemplate.setHashValueSerializer(stringRedisSerializer);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;

//        System.out.println(String.format("dbIndex: %s", dbIndex));

        return RedisConnectionConfig.createRedisTemplate(
                Integer.parseInt(dbIndex),
                hostName,
                Integer.parseInt(port),
                password,
                Integer.parseInt(maxIdle),
                Integer.parseInt(minIdle),
                Integer.parseInt(maxActive),
                Long.parseLong(maxWait),
                Long.parseLong(timeOut),
                Long.parseLong(shutdownTimeOut)
        );
    }
}
